package com.taotao.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.utiles.IDUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.service.ItemService;
import com.taotaotao.common.pojo.EasyUIDataGridResult;
import com.taotaotao.common.pojo.TaotaoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Override
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        //使用pageHelper完成分页查询
        PageHelper.startPage(page,rows);
        TbItemExample example = new TbItemExample();
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        //封装返回值
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        PageInfo<TbItem> info = new PageInfo<TbItem>(tbItems);
        result.setRows(tbItems);
        result.setTotal(info.getTotal());
        return result;
    }

    @Override
    public TaotaoResult addItem(TbItem item, String desc) {
        //补全item的其他属性
        long id = IDUtils.genItemId();
        item.setId(id);
        //商品状态,1-正常,2-下架,3-删除
        item.setStatus((byte)1);
        Date date = new Date();
        item.setCreated(date);
        item.setUpdated(date);
        //往数据库中插入商品
        itemMapper.insert(item);
        //补全商品描述表
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(id);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        //插入到数据库
        itemDescMapper.insert(itemDesc);

        return TaotaoResult.ok();
    }


}
